package com.example.spring_principle.controller;

import com.example.spring_principle.model.Dog;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Li_yizYa
 * Date: 2025/7/31
 * Time: 15:50
 */
@RestController
@RequestMapping("/scope")
public class ScopeController {
    @Resource(name = "singleDog") // 此处 name 是方法名
    private Dog singleDog;

    @Resource(name = "prototypeDog")
    private Dog prototypeDog;

    @Resource(name = "requestDog")
    private Dog requestDog;

    @Resource(name = "sessionDog")
    private Dog sessionDog;

    @Resource(name = "applicationDog")
    private Dog applicationDog;


    @Autowired
    private ApplicationContext context;

    @RequestMapping("/single")
    public String single() {
        // 从 context 中获取对象
        Dog dog = (Dog)context.getBean("singleDog");
        return "注入对象:" + singleDog.toString() + ", context Dog:" + dog.toString();
    }

    @RequestMapping("/prototype")
    public String prototype() {
        // 从 context 中获取对象
        Dog dog = (Dog)context.getBean("prototypeDog");
        return "注入对象:" + prototypeDog.toString() + ", context Dog:" + dog.toString();
    }

    @RequestMapping("/request")
    public String request() {
        // 从 context 中获取对象
        Dog dog = (Dog)context.getBean("requestDog");
        return "注入对象:" + requestDog.toString() + ", context Dog:" + dog.toString();
    }

    @RequestMapping("/applicationDog")
    public String session() {
        // 从 context 中获取对象
        Dog dog = (Dog)context.getBean("applicationDog");
        return "注入对象:" + applicationDog.toString() + ", context Dog:" + dog.toString();
    }

    @RequestMapping("/session")
    public String application() {
        // 从 context 中获取对象
        Dog dog = (Dog)context.getBean("sessionDog");
        return "注入对象:" + sessionDog.toString() + ", context Dog:" + dog.toString();
    }



}
